package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.greendeliver.business.ServiceManager;

public class ServiceMenuDetailActivity extends ListActivity
{

	private ArrayList<HashMap<String, String>>	servicesInfos;
	private String								ID_SERVICE;

	private ProgressDialog						pDialog;
	private Intent								i;

	// JSON Node names
	private static final String					TAG_SUCCESS				= "success";
	private static final String					TAG_EVENTS				= "events";
	private static final String					TAG_ID					= "ID_service";
	private static final String					TAG_ID_MENU				= "ID_menu";
	private static final String					TAG_NAME_MENU			= "name_menu";
	private static final String					TAG_DESCRIPTION_MENU	= "description_menu";
	private static final String					TAG_PRICE				= "Price";

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_service_menu_detail );

		this.servicesInfos = new ArrayList<HashMap<String, String>>();
		this.pDialog = new ProgressDialog( ServiceMenuDetailActivity.this );

		Intent i = getIntent();
		this.ID_SERVICE = i.getStringExtra( TAG_ID );

		Log.d( ID_SERVICE, "ID CONNARD" );

		new Load().execute();

		ListView lv = getListView();

		lv.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick( AdapterView<?> parent, View view,
					int position, long id ) {
				// getting values from selected ListItem
				String id_menu = ( (TextView) view.findViewById( R.id.id_menu ) ).getText().toString();
				String name_menu = ( (TextView) view.findViewById( R.id.name_menu ) ).getText().toString();

				// Starting new intent
				Intent in = new Intent( getApplicationContext(),
						MenuDetailActivity.class );
				// sending id to next activity
				in.putExtra( TAG_ID_MENU, id_menu );
				in.putExtra( TAG_NAME_MENU, name_menu );

				// starting new activity and expecting some response back
				startActivity( in );
			}
		} );
	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			Log.d( "Coucou", "1" );
			super.onPreExecute();
			pDialog.setMessage( "Chargement de(s) Menu(s)" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( false );
			pDialog.show();
			Log.d( "Coucou", "2" );
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			Log.d( "Coucou", "3" );
			ServiceManager sm = new ServiceManager();
			Log.d( "Coucou", "4" );
			servicesInfos = sm.get_menu_by_ID( ID_SERVICE );
			Log.d( "Coucou", "5" );

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			Log.d( "Coucou", "6" );
			pDialog.dismiss();
			Log.d( "Coucou", "7" );
			ListAdapter adapter = new SimpleAdapter( ServiceMenuDetailActivity.this,
					servicesInfos, R.layout.list_menu,
					new String[] { TAG_ID_MENU, TAG_NAME_MENU, TAG_DESCRIPTION_MENU, TAG_PRICE },
					new int[] { R.id.id_menu, R.id.name_menu, R.id.description_menu, R.id.price_menu } );
			setListAdapter( adapter );
			Log.d( "Coucou", "8" );
		}
	}

}
